//
//  CJSLunboView.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/24.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSLunboView.h"
#import "Lunbo.h"
#import "UIImageView+WebCache.h"

@interface CJSLunboView()<UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger pageNumber;

@property (nonatomic, retain) UIScrollView *scrol;

@property (nonatomic, retain) UIPageControl *page;

@property (nonatomic, assign) BOOL isCreate;

@property (nonatomic, retain) NSTimer *timer;

@end

@implementation CJSLunboView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.isCreate = NO;
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (void)createSubViews:(NSMutableArray *)lunboArray{
    
    self.pageNumber = lunboArray.count;
#pragma makr 轮播图
    self.scrol = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [lunboArray addObject:[lunboArray firstObject]];
    [lunboArray insertObject:[lunboArray objectAtIndex:lunboArray.count - 2] atIndex:0];
    self.scrol.contentSize = CGSizeMake(self.scrol.frame.size.width * lunboArray.count, self.scrol.frame.size.height);
    self.scrol.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    
    self.scrol.showsHorizontalScrollIndicator = NO;
    
    for (int i = 0; i < lunboArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[[lunboArray objectAtIndex:i] image_url]] placeholderImage:nil];
        imageView.frame = CGRectMake(self.scrol.frame.size.width * i, 0, self.scrol.frame.size.width, self.scrol.frame.size.height);
        imageView.userInteractionEnabled = YES;
        [self.scrol addSubview:imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
    }
    
    self.scrol.pagingEnabled = YES;
    self.scrol.tag = 100;
    self.scrol.bounces = NO;
    self.scrol.delegate = self;
    
    [self addSubview:self.scrol];
    
#pragma mark page
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, self.scrol.frame.size.height - 30, self.frame.size.width - 200, 30)];
    
    self.page.numberOfPages = self.pageNumber;
    self.page.tag = 50;
    self.page.currentPage = 0;
    //    page.backgroundColor = [UIColor grayColor];
    [self addSubview:self.page];
    
    [self.page addTarget:self action:@selector(actionPage:) forControlEvents:UIControlEventTouchUpInside];
    
    if (!_timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(lunbo) userInfo:nil repeats:YES];
        self.isCreate = YES;
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    UIImageView *imageView = (UIImageView *)tap.view;
    
    NSInteger number = (NSInteger)imageView.frame.origin.x / (NSInteger)SCREEN_WIDTH;
    
    [self.delegate getTapNumber:number];
}

- (void)lunbo{
    
    UIPageControl *page = (UIPageControl *)[self viewWithTag:50];
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:100];
    if (page.currentPage == self.pageNumber - 1) {
        
        page.currentPage = 0;
    }
    else{
        
        page.currentPage++;
    }
    
    [scroll setContentOffset:CGPointMake(SCREEN_WIDTH * (page.currentPage + 1), 0) animated:YES];
    
    
    
}

- (void)actionPage:(UIPageControl *)page{
    
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:100];
    
    [scroll setContentOffset:CGPointMake(scroll.frame.size.width * page.currentPage, 0) animated:YES];
    
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    UIPageControl *page = (UIPageControl *)[self viewWithTag:50];
    if (scrollView.contentOffset.x == SCREEN_WIDTH * (self.pageNumber + 1)) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * self.pageNumber, 0);
    }
    page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width - 1;
    
    if (!_timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(lunbo) userInfo:nil repeats:YES];
        self.isCreate = YES;
    }

}

@end
